package com.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.web.common.Result;
import com.web.entity.DiseaseLibrary;
import com.web.service.DiseaseLibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/library")
@CrossOrigin
public class DiseaseLibraryController {

    @Autowired
    private DiseaseLibraryService libraryService;

    @GetMapping("/list")
    public Result<IPage<DiseaseLibrary>> getLibraryList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String keyword) {

        IPage<DiseaseLibrary> result = libraryService.getLibraryList(page, size, type, keyword);
        return Result.success("获取病虫害库列表成功", result);
    }

    @GetMapping("/detail/{id}")
    public Result<DiseaseLibrary> getLibraryDetail(@PathVariable Long id) {
        DiseaseLibrary library = libraryService.getLibraryDetail(id);
        if (library != null) {
            return Result.success("获取病虫害详情成功", library);
        }
        return Result.fail("病虫害信息不存在");
    }
}
